<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'
import resize from './mixins/resize'

export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '300px'
        },
        chartData: {
            type: Object,
            default: () => { }
        }
    },
    data() {
        return {
            chart: null
        }
    },
    watch: {
        chartData: {
            handler(val) {
                if (val) {
                    this.$nextTick(() => {
                        this.initChart()
                    })
                }
            },
            deep: true
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart()
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    methods: {
        initChart() {
            this.chart = echarts.init(this.$el, 'macarons')

            this.chart.setOption({
                color: ['#0046A4', '#0071D2', '#1190FF', '#00BFFB', '#4DD2FC', '#2BFFF2', '#6DFF9F', '#F0FF43'],
                tooltip: {},
                // legend: [
                //     {
                //         data: this.categories.map(function(a) {
                //             return a.name
                //         })
                //     }
                // ],
                series: [{
                    type: 'graph',
                    layout: 'force',
                    animation: false,
                    label: {
                        show: 'true',
                        position: 'right',
                        formatter: '{b}'
                    },
                    draggable: true,
                    // eslint-disable-next-line no-unused-vars
                    data: this.chartData.nodes,
                    categories: [],
                    force: {
                        edgeLength: 5,
                        repulsion: 200,
                        gravity: 0.2
                    },
                    edges: this.chartData.links
                }
                ]
            })
        }
    }
}
</script>
